package com.jade.explorer.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.XMLpart;
import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;

public class NewProjectDialog extends Dialog {

	private Text nameText;
	private Text authorText;
	private Text companyText;
	private Text descText;
	private String _path;
	private String _name = "";
	private String _author = "";
	private String _company = "";
	private String _desc = "";
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public NewProjectDialog(Shell parentShell, String path) {
		super(parentShell);
		_path = path;
	}

	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		//----title
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("New Project", 10, 10, true); 
				} 
		}); 
		//---
		
		
		
		
		
		
		
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBounds(0, 43, 444, 206);
		
		Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setBounds(10, 11, 73, 15);
		nameLabel.setText("Name:");
		
		Label authorLabel = new Label(composite, SWT.NONE);
		authorLabel.setBounds(10, 35, 73, 15);
		authorLabel.setText("Author:");
		
		Label companyLabel = new Label(composite, SWT.NONE);
		companyLabel.setBounds(10, 57, 73, 15);
		companyLabel.setText("Company:");
		
		Label descriptionLabel = new Label(composite, SWT.NONE);
		descriptionLabel.setBounds(10, 85, 73, 15);
		descriptionLabel.setText("Desription:");
		
		nameText = new Text(composite, SWT.BORDER);
		nameText.setBounds(89, 7, 345, 21);
		nameText.setTextLimit(50);
		nameText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_name = nameText.getText();
			}
		});
		
		authorText = new Text(composite, SWT.BORDER);
		authorText.setBounds(89, 31, 345, 21);
		authorText.setTextLimit(50);
		authorText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_author = authorText.getText();
			}
		});
		
		companyText = new Text(composite, SWT.BORDER);
		companyText.setBounds(89, 55, 345, 21);
		companyText.setTextLimit(50);
		companyText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_company = companyText.getText();
			}
		});
		
		descText = new Text(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		descText.setBounds(89, 82, 345, 114);
		descText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_desc = descText.getText();
			}
		});
		
		
		
		
		
		

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Shell shell = super.getShell();
		Button oKButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
		oKButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if (FileSystem.checkInput(_name)){
					if (!_name.equals("")){
						
						if(FileSystem.createProjectSctucture(_path, _name)){
							Date dNow = new Date( );
							SimpleDateFormat ft = 
							new SimpleDateFormat ("HH:mm',' dd.MM.yyyy");
							
							HashMap<String, String> data = new HashMap<String, String>();
							data.put("name", _name);
							data.put("author", _author);
							data.put("company", _company);
							data.put("created", ft.format(dNow));
							data.put("desc", _desc);
							String prop = _path + "\\" + _name + "\\.project";
							if(!XMLpart.writeProjectPropertyFile(prop, data)){
								_name = "";
							}
						}
						else{
							_name = "";
						}
						
						close();
					}
					else{
						MessageDialog.openError(shell, "Error", "Enter file name!");
						open();
					}
					close();
				}
				else{
					MessageDialog.openError(shell, "Error", "File name cant contain any of following characters:\n< > / \\ * : ; ? \" ' | .");
					_name = "";
					open();
				}
				
				close();
			}
		});
		Button cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				close();
			}
		});
	}

	
	public String getName(){
		return _name;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 328);
	}
}
